package merchant.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import merchant.dao.Merchant;
import merchant.dao.MerchantHierarchyLevelNumber;
import merchant.entity.MerchantSettlementDetail;
import merchant.entity.RecordOfCharge;
import merchant.dao.Status;
import merchant.entity.SummaryOfCharge;
import utils.MerchantDAO;

public class Runner {

	
	final static Map<String,Long> settlementIDMap = new HashMap<String,Long>();
	final static Map<String,Long> socIDMap = new HashMap<String,Long>();
	final static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	
	public static void main(String[] args) throws Exception {
		//final MerchantDAO dao = new MerchantDAO("http://localhost:8888");
		final MerchantDAO dao = new MerchantDAO("http://hackathon1212.appspot.com");
		final List<Merchant> ms = getMerchantData();
		for(final Merchant m : ms){
			dao.saveMerchant(m);
		}
		/*
		final List<MerchantSettlementDetail> ss = getSettlementData();
		for(final MerchantSettlementDetail s : ss){
			final merchant.dao.MerchantSettlementDetail s2 = dao.saveSettlement(s);
			settlementIDMap.put(s.getOldID(), s2.getSettlementId());
			System.out.println("SM:" + s.getOldID() +","+ s2.getSettlementId());
		}
		
		final List<SummaryOfCharge> socs = getSoCData();
		for(final SummaryOfCharge soc : socs){
			final String lookup = String.format("S%1$09d", soc.getSettlementId());
			soc.setSettlementId(settlementIDMap.get(lookup));
			final SummaryOfCharge soc2 = dao.saveSoC(soc);
			socIDMap.put(soc.getOldID(), soc2.getSocId());
			System.out.println("socM:" + soc.getOldID() +","+ soc2.getSocId());
		}
		
		final List<RecordOfCharge> rocs = getRoCData();
		for(final RecordOfCharge roc : rocs){
			roc.setSocId(socIDMap.get("SOC" + roc.getSocId()));
			final RecordOfCharge roc2 = dao.saveRoC(roc);
		}*/
	}
	
	public static List<Merchant> getMerchantData() throws IOException{
		final BufferedReader in
		   = new BufferedReader(new FileReader("merchant.csv"));
		String line;
		final List<Merchant> ret = new LinkedList<Merchant>();
		while ((line = in.readLine()) != null){
			final String[] split = line.split(",");
			
			final Merchant m = createMerchant(split);
			ret.add(m);
		}
		return ret;
	}

	
	private static final int merchantNumber = 0;	
	private static final int merchantBusinessCentre = 1;
	private static final int merchantHierarchyLevelNumber = 2;
	private static final int superiorMerchantNumber = 3;
	private static final int doingBusinessAsName = 4;
	private static final int industryMajor = 5;
	private static final int industryDetail = 6;
	private static final int status = 7;
	private static final int languagePreference = 8;
	private static final int roleType = 9;
	private static final int submissionCurrency = 10;
	private static final int settlementCurrency = 11;
	private static final int telephoneNumber = 12;
	private static final int addressLine1 = 13;
	private static final int addressline2 = 14;
	private static final int addressline3 = 15;
	private static final int addressline4 = 16;
	private static final int city = 17;
	private static final int country = 18;
	private static final int postcode = 19;
	private static final int clientManagerName = 20;
	
	private static Merchant createMerchant(final String[] split) {
		
		final Merchant ret = new Merchant(split[merchantNumber], split[merchantBusinessCentre],
				MerchantHierarchyLevelNumber.valueOf("Level_0" + split[merchantHierarchyLevelNumber]),
				split[superiorMerchantNumber], split[doingBusinessAsName],
				split[industryMajor],split[industryDetail], split[status], split[languagePreference],
				split[roleType], split[submissionCurrency],
				split[settlementCurrency], split[telephoneNumber], split[addressLine1],
				split[addressline2], split[addressline3], split[addressline4],
				split[city],split[country],split[postcode], split[clientManagerName]);
		
		return ret;
	}
	
	public static List<MerchantSettlementDetail> getSettlementData() throws IOException, NumberFormatException, ParseException{
		final BufferedReader in
		   = new BufferedReader(new FileReader("settlement.csv"));
		String line;
		final List<MerchantSettlementDetail> ret = new LinkedList<MerchantSettlementDetail>();
		while ((line = in.readLine()) != null){
			final String[] split = line.split(",");
			
			final MerchantSettlementDetail m = createSettlement(split);
			ret.add(m);
		}
		return ret;
	}

	
	private static final int settlementId = 0;	
	private static final int s_merchantNumber = 1;
	private static final int s_merchantBusinessCentre = 2;
	private static final int settlementDate = 3;
	private static final int s_settlementCurrency = 4;
	private static final int settlementLocalAmount = 5;
	private static final int settlementUSDAmount = 6;
	
	private static MerchantSettlementDetail createSettlement(final String[] split) throws NumberFormatException, ParseException {
		final MerchantSettlementDetail ret = new MerchantSettlementDetail(
				split[s_merchantNumber],
				split[s_merchantBusinessCentre],
				sdf.parse(split[settlementDate]),
				split[s_settlementCurrency]
				);
		
		ret.setOldID(split[settlementId]);
		
		return ret;
	}
	
	public static List<SummaryOfCharge> getSoCData() throws IOException, NumberFormatException, ParseException{
		final BufferedReader in
		   = new BufferedReader(new FileReader("soc.csv"));
		String line;
		final List<SummaryOfCharge> ret = new LinkedList<SummaryOfCharge>();
		while ((line = in.readLine()) != null){
			final String[] split = line.split(",");
			
			final SummaryOfCharge m = createSoC(split);
			ret.add(m);
		}
		return ret;
	}

	private static final int soc_settlementId = 0;
	private static final int soc_socId = 1;
	private static final int processingDate = 2;
	private static final int rocCount = 3;
	private static final int socCurrency = 4;
	private static final int amount = 5;
	private static final int discountAmount = 6;
	
	private static SummaryOfCharge createSoC(final String[] split) throws NumberFormatException, ParseException {

		final SummaryOfCharge ret = new SummaryOfCharge(
				Long.parseLong(split[soc_settlementId].replace("S", "")),
				sdf.parse(split[processingDate]),
				Integer.parseInt(split[rocCount]),
				split[socCurrency],
				Float.parseFloat(split[amount]));
		
		ret.setOldID(split[soc_socId]);
		
		return ret;
	}
	
	public static List<RecordOfCharge> getRoCData() throws IOException, NumberFormatException, ParseException{
		final BufferedReader in
		   = new BufferedReader(new FileReader("roc.csv"));
		String line;
		final List<RecordOfCharge> ret = new LinkedList<RecordOfCharge>();
		while ((line = in.readLine()) != null){
			final String[] split = line.split(",");
			
			final RecordOfCharge m = createRoC(split);
			ret.add(m);
		}
		return ret;
	}
	
	private static final int socId = 0;
	private static final int roc_merchantNumber = 2;
	private static final int submittedCurrencyType = 3;
	private static final int submittedLocalAmount = 4;
	//private static final int usdAmount = 4;
	private static final int pan = 5;
	private static final int date = 6;
	
	private static RecordOfCharge createRoC(final String[] split) throws NumberFormatException, ParseException {

		final RecordOfCharge ret = new RecordOfCharge(Long.parseLong(split[socId].replaceAll("SOC", "")),
				split[roc_merchantNumber],
				split[submittedCurrencyType],
				Float.parseFloat(split[submittedLocalAmount]),
				split[pan],
				sdf.parse(split[date]));		
		return ret;
	}

}
